package cn.edu.scau.dao;

import cn.edu.scau.entity.WorkPlan;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.ibatis.jdbc.SQL;

public class WorkPlanSqlProvider {

    public String insertSelective(WorkPlan record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("workplan");
        
        if (record.getWorkDate() != null) {
            sql.VALUES("workDate", "#{workDate,jdbcType=DATE}");
        }
        
        if (record.getNurseID() != null) {
            sql.VALUES("nurseID", "#{nurseID,jdbcType=INTEGER}");
        }
        
        if (record.getWorkMorning() != null) {
            sql.VALUES("workMorning", "#{workMorning,jdbcType=BIT}");
        }
        
        if (record.getWorkAfternoon() != null) {
            sql.VALUES("workAfternoon", "#{workAfternoon,jdbcType=BIT}");
        }
        
        if (record.getWorkEvening() != null) {
            sql.VALUES("workEvening", "#{workEvening,jdbcType=BIT}");
        }
        
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(WorkPlan record) {
        SQL sql = new SQL();
        sql.UPDATE("workplan");
        
        if (record.getWorkMorning() != null) {
            sql.SET("workMorning = #{workMorning,jdbcType=BIT}");
        }
        
        if (record.getWorkAfternoon() != null) {
            sql.SET("workAfternoon = #{workAfternoon,jdbcType=BIT}");
        }
        
        if (record.getWorkEvening() != null) {
            sql.SET("workEvening = #{workEvening,jdbcType=BIT}");
        }
        
        sql.WHERE("workDate = #{workDate,jdbcType=DATE}");
        sql.WHERE("nurseID = #{nurseID,jdbcType=INTEGER}");
        
        return sql.toString();
    }
    
    public String find(Date workDate,Integer nurseID,String nurseName){
        SQL sql = new SQL();
        sql.SELECT("workDate, nurseID, nurseName, workMorning, workAfternoon, workEvening");
        sql.FROM("workplan natural join nurse");
        if(workDate!=null){
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            sql.WHERE(" workDate = \""+sdf.format(workDate)+"\"");
            System.out.println(sdf.format(workDate));
        }
        if(nurseID!=null){
            sql.WHERE(" nurseID like concat('%',#{nurseID},'%')");
        }
        if(nurseName!=null){
            sql.WHERE(" nurseName like concat('%',#{nurseName},'%')");
        }
        System.out.println(sql.toString());
        return sql.toString();
        
    }
    
    public String findWithNurseID(Date workDate,Integer nurseID,String nurseName){
        SQL sql = new SQL();
        sql.SELECT("workDate, nurseID, nurseName, workMorning, workAfternoon, workEvening");
        sql.FROM("workplan natural join nurse");
        if(workDate!=null){
        	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            sql.WHERE(" workDate = \""+sdf.format(workDate)+"\"");
            System.out.println(sdf.format(workDate));
        }
        if(nurseID!=null){
            sql.WHERE(" nurseID = #{nurseID} ");
        }
        if(nurseName!=null){
            sql.WHERE(" nurseName like concat('%',#{nurseName},'%')");
        }
        System.out.println(sql.toString());
        return sql.toString();
        
    }
}